﻿using GlennQuigmire.API.Application.GDtos;
using IMES.Common.Domain.SirModel;
using IMES.Common.Dtos;
using IMES.Common.Infrastructure;
using MediatR;

namespace GlennQuigmire.API.Application.Commands
{
    public class OutboundOrderSearchHander : IRequestHandler<OutboundOrderSearchCommand, ResultT<OutboundOrderListDto>>
    {

        private readonly IBaseRepositorys<OutboundOrder> outboundOrderRepository;
        private readonly IBaseRepositorys<OutboundOrderType> outboundOrderTypeRepository;

        public OutboundOrderSearchHander(IBaseRepositorys<OutboundOrder> outboundOrderRepository, IBaseRepositorys<OutboundOrderType> outboundOrderTypeRepository)
        {
            this.outboundOrderRepository = outboundOrderRepository;
            this.outboundOrderTypeRepository = outboundOrderTypeRepository;
        }


        /// <summary>
        /// 查询出库单列表
        /// </summary>
        /// <param name="request">查询出库单列表请求对象</param>
        /// <param name="cancellationToken">允许取消的Token</param>
        /// <returns>查询出库单列表结果对象</returns>
        public Task<ResultT<OutboundOrderListDto>> Handle(OutboundOrderSearchCommand request, CancellationToken cancellationToken)
        {
            var resutl = new ResultT<OutboundOrderListDto>();

            var list = (from a in outboundOrderRepository.GetAll()
                        join b in outboundOrderTypeRepository.GetAll() on a.OutboundOrderTypeId equals b.OutboundOrderTypeId select new OutboundOrderListDto
                        {
                            OutboundOrderId = a.OutboundOrderId,
                            OutboundOrderTypeId = a.OutboundOrderTypeId,
                            OutboundOrderNumber = a.OutboundOrderNumber,
                            OutboundOrderTypeName = b.OutboundOrderTypeName,
                            OutbroundOrderDate = a.OutbroundOrderDate.ToString("yyyy-MM-dd HH:mm:ss"),
                            Remark=a.Remark,
                            FileUrl=a.FileUrl,
                            ImageUrl=a.ImageUrl,
                            ExamineStatus=a.ExamineStatus,
                            CreateId = a.CreateId,
                            CreateDate = a.CreateDate.ToString("yyyy-MM-dd HH:mm:ss"),
                            CreateName = a.CreateName,
                            UpdateId = a.UpdateId,
                            UpdateDate = a.UpdateDate,
                            UpdateName = a.UpdateName,
                        }).ToList();
            if(!string.IsNullOrEmpty(request.OutboundOrderNumber))
            {
                list = list.Where(a => a.OutboundOrderNumber.Contains(request.OutboundOrderNumber)).ToList();
            }

            resutl.Data = list;
            return Task.FromResult(resutl);

        }
    }
}
